import {Component} from '@angular/core';
import {IonicPage, LoadingController, NavController, NavParams} from 'ionic-angular';
import {HttpClient} from "@angular/common/http";
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {ValidateProvider} from "../../providers/validate/validate";

@IonicPage({
    segment: 'service/operation',
    defaultHistory: ['ServicePage']
})
@Component({
    selector: 'page-operation',
    templateUrl: 'operation.html',
})
export class OperationPage {
    page = 1;
    infolist = [];
    loadObj: any;
    imagelist = [];
    navlist = [];
    nav_id: number = 24;
    banners = [];
    notices = [];

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public http: HttpClient,
        public loadingCtrl: LoadingController,
        public httpService: HttpServicesProvider,
        public validate: ValidateProvider
    ) {
        if (!this.validate.checkLogin()) {
            this.navCtrl.push('LoginPage');
        }
    }
    ionViewCanEnter(){
        return this.validate.checkLogin();
    }
    ionViewWillEnter(){
        this.page = 1;
        this.nav_id = this.navParams.get('nav_id');
        if(this.nav_id == undefined){
            this.nav_id = 24;
        }
        this.loadObj = this.loadingCtrl.create();
        this.loadObj.present();
        this.getData(null);
    }

    getData(infiniteScroll) {
        this.httpService.requestData('/service/get_ads/type_id/' + this.nav_id, response => {
            let data = response['data'];
            this.banners = data['image_list'];
            this.notices = data['text_list'];
        });

        this.httpService.requestData('/service/get_info_list/type_id/' + this.nav_id + '/page/' + this.page, response => {
            let data = response['data'];
            this.infolist = this.infolist.concat(data['info_list']['lists']);
            this.page++;
            if (infiniteScroll) {
                infiniteScroll.complete();
                /*请求完成数据更新页面*/
                if (data['info_list']['lists'].length < data['info_list']['page_size']) {   /*没有数据 禁用上拉更新*/
                    infiniteScroll.enable(false);
                }
            } else {
                this.loadObj.dismiss();
                this.infolist = data['info_list']['lists'];
                this.imagelist = data['image_list'];
                this.navlist = data['nav'];
            }
        });
    }

    doInfinite(infiniteScroll) {
        this.getData(infiniteScroll);
    }


}
